﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Laboratorium1
{
    public partial class Form1 : Form
    {
        private Device device;
        private MyObject myObject;
        private float t = 0;
           

        public Form1()
        {
            InitializeComponent();
        }

        public void InitializeDevice()
        {
            PresentParameters parameters = new PresentParameters();
            parameters.Windowed = true;
            parameters.SwapEffect = SwapEffect.Discard;
            parameters.AutoDepthStencilFormat = DepthFormat.D16;
            parameters.EnableAutoDepthStencil = true;

            device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, parameters);
        }

        public void InitializeCamera() // To jest nowe w tej wersji. Tworzymy kamerę.
        {
            float aspect_ratio = this.Width / (float)this.Height;

            float q = 2.0f + (float)Math.Sin(4 * t); //q - pomocnicza zmienna sterująca ruchem kamery
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, aspect_ratio, 1f, myObject.radius * 12);  // macierz projekcji
            device.Transform.View = Matrix.LookAtLH(
                myObject.center - (new Vector3(3 * myObject.radius * (float)Math.Cos(t), 3 * myObject.radius * (float)Math.Sin(t), (float)3 * q * myObject.radius * (float)Math.Sin(t))),
                myObject.center,
                new Vector3(0, 1, 0)); // macierz widoku

            device.Transform.World = Matrix.Identity;

            device.Lights[0].Type = LightType.Directional;  // tworzymy dwa światła kierunkowe
            device.Lights[0].Diffuse = Color.White;
            device.Lights[0].Direction = new Vector3(1, 1, 1);
            device.Lights[0].Enabled = true;
            device.Lights[1].Type = LightType.Directional;
            device.Lights[1].Diffuse = Color.White;
            device.Lights[1].Direction = new Vector3(-1, -1, -1);
            device.Lights[1].Enabled = true;
        }

        public void InitializeMeshes()  // nowa metoda wczytująca obiekt
        {
            myObject = new MyObject(ref device);
            myObject.LoadMesh();
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) //Trzecia nowość. W zdarzeniu OnPaint będziemy rysować. 
        {

            InitializeCamera(); //inicjalizacji kamerki
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.BlueViolet, 1.0f, 0);


            device.BeginScene(); // początek sceny 
            myObject.Render();
            device.EndScene();  //koniec sceny

            t += 0.02f;
            device.Present();

            this.Invalidate(); //to po to aby windows przerysował formę
        }
    }
}
